#!/usr/bin/python
# -*- coding: iso-8859-3 -*-

import sys, time, serial

from PyQt4 import QtCore, QtGui

from DialogoLed_Ui import Ui_DialogoLed

# Variables Globales



class DialogoLed(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(DialogoLed, self).__init__(parent)
        #QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_DialogoLed()
        self.ui.setupUi(self)
        
        self.stadoled01 = 0
        
        self.arduino = serial.Serial('/dev/ttyACM0', 9600)
        
        txtArduino = self.arduino.readline()
        txt = "Arduino> " +txtArduino
        self.ui.textBrowser.append(txt)
 
 
	#buttons
        QtCore.QObject.connect(self.ui.pushButton,QtCore.SIGNAL("clicked()"), self.led01)
        

    def led01(self):
        if self.stadoled01==0:
	  self.stadoled01 = 1
	  self.ui.pushButton.setText('Apagar Led')
	  
	  self.arduino.write('Y')
          txtArduino = self.arduino.readline()
          
	  
	else:
	  self.stadoled01 = 0
	  self.ui.pushButton.setText ('Encender Led')
	  self.arduino.write('N')
          txtArduino = self.arduino.readline()
          
	
	txt = "Arduino> " +txtArduino
        self.ui.textBrowser.append(txt)
        
    def quit(self):
	sys.exit(app.exec_())
   
    def closeEvent(self, event):
        self.arduino.close();
	sys.exit(app.exec_())


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = DialogoLed()
    myapp.show()
    sys.exit(app.exec_())
